<template>
  <div id="app">
    <el-row>
      <el-col :span="24">
        <el-card class="box-card">
          <template #header>
            <div class="card-header">
              <span>食物列表</span>
              <el-button type="primary" @click="handleAddFood">添加食物</el-button>
            </div>
          </template>
          <el-table :data="foods" border style="width: 100%">
            <el-table-column prop="name" label="食物名称">
            </el-table-column>
            <el-table-column prop="description" label="食物描述">
            </el-table-column>
            <el-table-column prop="price" label="食物价格">
            </el-table-column>
<!--            <el-table-column label="操作" width="180">-->
<!--              <template #default="scope">-->
<!--                <el-button type="primary" size="small" @click="handleEditFood(scope.row)">编辑</el-button>-->
<!--                <el-button type="danger" size="small" @click="handleDeleteFood(scope.row)">删除</el-button>-->
<!--              </template>-->
<!--            </el-table-column>-->
          </el-table>
        </el-card>
      </el-col>
    </el-row>

    <el-dialog v-model="dialogVisible" title="添加食物" width="30%">
      <el-form :model="form" label-width="80px">
        <el-form-item label="食物名称">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="食物描述">
          <el-input v-model="form.description" />
        </el-form-item>
        <el-form-item label="食物价格">
          <el-input v-model="form.price" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSaveFood">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import axios from 'axios';
import { ElMessage } from 'element-plus';

export default {
  name: 'AdminFood',
  data() {
    return {
      foods: [],
      dialogVisible: false,
      form: {
        name: '',
        description: '',
        price: 0,
      },
    };
  },
  mounted() {
    this.fetchFoods();
  },
  methods: {
    fetchFoods() {
      axios.get('/food')
          .then(response => {
            this.foods = response.data;
          })
          .catch(error => {
            console.error('获取食物列表失败', error);
            ElMessage.error('获取食物列表失败');
          });
    },
    handleAddFood() {
      this.dialogVisible = true;
      this.form = {
        name: '',
        description: '',
        price: 0,
      };
    },
    handleSaveFood() {
      axios.post('/food', this.form)
          .then(response => {
            console.log('添加食物成功', response.data);
            this.dialogVisible = false;
            this.fetchFoods();
            ElMessage.success('添加食物成功');
          })
          .catch((error) => {
            console.error('添加食物失败', error);
            ElMessage.error('添加食物失败');
          });
    },
    // handleEditFood(food) {
    //   // TODO: 实现编辑食物功能
    //   console.log('编辑食物', food);
    // },
    // handleDeleteFood(food) {
    //   // TODO: 实现删除城市功能
    //   console.log('删除食物', food);
    // },
  },
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}

.box-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
